package com.jimikongjian.configuration;

import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

/**
 * Created by fuxiuyang on 17-6-16.
 */

@Component
public class KaptchaConfig {

    @Value("${code.border.isBorder}")
    private String border;

    @Value("${code.border.color}")
    private String borderColor;

    @Value("${code.textproducer.font.color}")
    private String fontColor;

    @Value("${code.image.width}")
    private String imageWidth;

    @Value("${code.image.height}")
    private String imageHeight;

    @Value("${code.textproducer.font.size}")
    private String fontSize;

    @Value("${code.session.key}")
    private String sessionKey;

    @Value("${code.textproducer.char.length}")
    private String charLength;

    @Value("${code.textproducer.font.names}")
    private String fontNames;

    @Bean
    public DefaultKaptcha getDefaultKaptcha(){
        com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
        Properties properties = new Properties();
        properties.setProperty("kaptcha.border", border);
        properties.setProperty("kaptcha.border.color", borderColor);
        properties.setProperty("kaptcha.textproducer.font.color", fontColor);
        properties.setProperty("kaptcha.image.width", imageWidth);
        properties.setProperty("kaptcha.image.height", imageHeight);
        properties.setProperty("kaptcha.textproducer.font.size", fontSize);
        properties.setProperty("kaptcha.session.key", sessionKey);
        properties.setProperty("kaptcha.textproducer.char.length", charLength);
        properties.setProperty("kaptcha.textproducer.font.names", fontNames);
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);

        return defaultKaptcha;
    }
}
